const db = wx.cloud.database()
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        haveEnrolled: false, // 是否报名活动
        cannotEnroll: true // 是否不可报名
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        let that = this
        const eventChannel = this.getOpenerEventChannel()
        eventChannel.on('acceptDataFromOpenerPage', function(data) {
            let id = data.id
            that.setData({
                id: id
            })
            db.collection("activityDB").doc(id).get({
                success: function(res) {
                  console.log(res.data);
                    let markers = [{
                        latitude: res.data.latitude,
                        longitude: res.data.longitude,
                        title: res.data.location
                    }]
                    let beginTime = new Date(res.data.beginTime)
                    let endTime = new Date(res.data.endTime)
                    if ( app.globalData.openid in res.data.people  ) {
                        that.setData({
                            haveEnrolled: true
                        })
                    }
                    if ( Object.keys(res.data.people).length < res.data.maxpeople ) {
                        that.setData({
                            cannotEnroll: false
                        })
                    }
                    that.setData(res.data)

                    // 获取发起人信息
                    db.collection("userDB").where({
                        _openid: res.data._openid 
                    }).get({
                        success: (res) => {
                          console.log(res);
                            that.setData({
                                avatar: res.data[0].avatar,
                                nickname: res.data[0].nickname
                            })
                        }
                    })
                    
                    that.setData({
                        beginYear: beginTime.getFullYear(),
                        beginMonth: beginTime.getMonth() + 1,
                        beginDay: beginTime.getDate(),
                        beginDayOfWeek: ["星期日","星期一","星期二","星期三","星期四","星期五","星期六"][beginTime.getDay()],
                        endYear: endTime.getFullYear(),
                        endMonth: endTime.getMonth() + 1,
                        endDay: endTime.getDate(),
                        endDayOfWeek: ["星期日","星期一","星期二","星期三","星期四","星期五","星期六"][endTime.getDay()],
                        beginHour: (beginTime.getHours() < 10 ? '0' + beginTime.getHours() : beginTime.getHours()),
                        beginMinute: (beginTime.getMinutes() < 10 ? '0' + beginTime.getMinutes() : beginTime.getMinutes()),
                        endHour: (endTime.getHours() < 10 ? '0' + endTime.getHours() : endTime.getHours()),
                        endMinute: (endTime.getMinutes() < 10 ? '0' + endTime.getMinutes() : endTime.getMinutes()),
                        between: (endTime - beginTime) / 60 / 60 / 1000,
                        people: Object.keys(res.data.people).length,
                        markers,
                        
                    })
                }
            })
        })
    },
    // 报名活动
    enroll(e) {
        let that = this
        wx.showLoading({title:"报名中..."})
        this.setData({
            cannotEnroll: true
        })
        wx.requestSubscribeMessage({
            tmplIds: ["hzp3AiWK1_DZ8br2eL8XjUgoX2wxWTtYtTyD1a04E_o"],
            success: function(res) {
                console.log("订阅成功！",res);
            },
            fail: function(res) {
                console.log(res);
            }
        })
        db.collection("activityDB").doc(this.data.id).update({
            data: {
                people: db.command.set(
                    {[`${app.globalData.openid}`]: {"checked":false,"pushed":false}}
                ) 
            },
            success: function(res) {
                wx.showToast({
                    title: '报名成功！',
                    icon: 'none',
                    duration: 2000
                })
                that.setData({
                    people: that.data.people + 1,
                    haveEnrolled: true
                })
                setTimeout(function(res){
                    that.setData({
                        cannotEnroll: false
                    })
                },2000)
            },
            fail: function(res) {
                wx.showToast({
                    title: '报名失败！请检查网络！',
                    icon: 'error',
                    duration: 2000
                })
                that.setData({
                    cannotEnroll: false
                })
            }
        })
    },
    cancelEnrolled(e) {
        let that = this
        wx.showLoading({title:'取消中...'})
        this.setData({
            cannotEnroll: true
        })
        db.collection("activityDB").doc(this.data.id).update({
            data: {
                [`people.${app.globalData.openid}`]: db.command.remove()
            },
            success: function(res) {
                wx.showToast({
                    title: '取消成功！',
                    icon: 'none',
                    duration: 2000
                })
                that.setData({
                    people: that.data.people - 1,
                    haveEnrolled: false
                })
                setTimeout(function(res){
                    that.setData({
                        cannotEnroll: false
                    })
                },2000)
            },
            fail: function(res) {
                wx.showToast({
                    title: '取消失败！请检查网络！',
                    icon: 'error',
                    duration: 2000
                })
                that.setData({
                    cannotEnroll: false
                })
            }
        })
    },
    gotoBoardGame(e) {
        let id = e.currentTarget.dataset.id
        wx.navigateTo({
            url: '../BoardGame/BoardGame',
            success: function(res) {
                res.eventChannel.emit('acceptDataFromOpenerPage',{id: id})
            }
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})